package net.siren93.smh.action;

import net.siren93.smh.entity.ProjectEntity;
import net.siren93.smh.entity.request.ProjectsRequest;
import net.siren93.smh.entity.response.BaseResponse;
import net.siren93.smh.entity.response.ProjectPageResponse;
import net.siren93.smh.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by siren93 on 15/10/7.
 */
@Controller
@RequestMapping("/project")
public class ProjectAction {

    @Autowired
    ProjectService projectService;


    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String mainPage(){
        return "/main";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String projectList(){
        return "/projects";
    }

    @RequestMapping(value = "/projectList", method = RequestMethod.POST)
    public @ResponseBody
    ProjectPageResponse listProject(@CookieValue(value = "id") int id,
                                    @RequestParam(value = "page", defaultValue = "1") int page,
                                    @RequestParam(value = "rows", defaultValue = "10") int rows){
        ProjectPageResponse pageResponse = new ProjectPageResponse();
        List<ProjectEntity> list = projectService.findByManager(id , page, rows);
        pageResponse.setTotal(projectService.listProject().size());
        pageResponse.setRows(list);
        return pageResponse;
    }

    @RequestMapping(value = "/addOrUpdateProjects", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse addProject(@CookieValue(value = "id", defaultValue = "0") int id, @RequestBody ProjectsRequest[] projects) {
        if(id == 0) return new BaseResponse(BaseResponse.STATUS_FAILED, "您还没有登陆");
        try{
            for(ProjectsRequest request : projects) {
                ProjectEntity entity = new ProjectEntity();
                entity.setId(request.getId());
                entity.setName(request.getName());
                entity.setStatus(request.getStatus());
                String dateStr = request.getCreateDate();
                SimpleDateFormat fm = new SimpleDateFormat("MM/dd/yy");
                java.util.Date date = fm.parse(dateStr);
                entity.setCreateDate(new Date(date.getTime()));
                entity.setManagerId(id);
                projectService.saveOrUpdate(entity);
            }
            return new BaseResponse(BaseResponse.STATUS_SUCCESS, "成功");
        } catch (Exception e) {
            return new BaseResponse(BaseResponse.STATUS_FAILED, e.getMessage());
        }
    }

    @RequestMapping(value = "/delProjects", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse delProject(@CookieValue(value = "id", defaultValue = "0") int id, @RequestBody int[] projects) {
        if(id == 0) return new BaseResponse(BaseResponse.STATUS_FAILED, "您还没有登陆");
        try{
            for(int i : projects) {
                projectService.delById(i);
            }
            return new BaseResponse(BaseResponse.STATUS_SUCCESS, "成功");
        } catch (Exception e) {
            return new BaseResponse(BaseResponse.STATUS_FAILED, e.getMessage());
        }
    }

}
